package com.w.wx.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.dfa.WordTree;
import com.alibaba.fastjson.JSONObject;
import com.w.wx.domain.Goods;
import com.w.wx.domain.Msg;
import com.w.wx.domain.Notice;
import com.w.wx.domain.UserEr;
import com.w.wx.mapper.NoticeMapper;
import com.w.wx.service.UserService;
import com.w.wx.service.impls.GoodServiceSImpl;
import com.w.wx.utils.ALToHMUtil;
import com.w.wx.utils.SensitivewordFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@RestController()
@RequestMapping("/wx/goods")
@Slf4j
public class CoodsContro {

    @Autowired
    private GoodServiceSImpl goodServiceS;
    @Autowired
    private UserService userService;
    @Autowired
    private NoticeMapper noticeMapper;

    @PostMapping("/goodsIssue")
    public Msg goodsIssue(@RequestBody Goods goodinfo) {
        //判断用户是否被拉黑
        UserEr userInfo = userService.getUserInfo(goodinfo.getOpenid());
        if (userInfo.getState() == 10){
            Notice notice =noticeMapper.selectByTwoOpenid(userInfo.getOpenid());
            return Msg.fail().add("notice",notice);
        }


        //加载敏感词库
        SensitivewordFilter  filter = new SensitivewordFilter();

        Set<String> goods_name = filter.getSensitiveWord(goodinfo.getGoodsName(), 1);
        Set<String> goods_title = filter.getSensitiveWord(goodinfo.getGoodsTitle(), 1);

        if (!goods_name.isEmpty() && !goods_title.isEmpty() ){
            return Msg.fail().add("error","物品名称中敏感词:"+goods_name.toString()+"物品标题中敏感词:"+goods_title.toString());
        }
        if(!goods_name.isEmpty()){
            return Msg.fail().add("error","物品中名称敏感词:"+goods_name.toString());
        }
        if(!goods_title.isEmpty() ){
            return Msg.fail().add("error","物品标题中敏感词:"+goods_title.toString());
        }
        return ALToHMUtil.retrunEasy(goodServiceS.goodsIssue(goodinfo));

    }

    /**
     * 10条一页
     * @param state
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getSimpleGoods")
        public Msg getSimGoodsInfo(Integer state, @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        if (state==null){
            return Msg.fail().add("error","参数为空");
        }
        return ALToHMUtil.listTomap(goodServiceS.getSimpleGoods(state,pageNum,pageSize));
    }

    /**
     * 获取用户发布全部商品
     * @param openid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getAllSimpleGoods")
    public Msg getAllSimGoodsInfo(String openid, @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        if (openid==null){
            return Msg.fail().add("error","参数为空");
        }
        return ALToHMUtil.listTomap(goodServiceS.getAllSimpleGoods(openid,1,pageNum,pageSize));
    }

    @RequestMapping("/getDeGoodfwl")
    public Msg getDeGoodsInfo(Integer gid){
        if (gid==null){
            return Msg.fail().add("error","参数为空");
        }
        return ALToHMUtil.goodsDetail(goodServiceS.getDeGoodsFwl(gid));
    }

    @RequestMapping("/getDeGood")
    public Msg getDeGoodsByGid(Integer gid){
        if (gid==null){
            return Msg.fail().add("error","参数为空");
        }
        if (goodServiceS.getDetailByGid(gid)==null){
            return Msg.fail().add("error","该物品已被发布者删除！");
        }else{
            return ALToHMUtil.goodsDetail(goodServiceS.getDetailByGid(gid));
        }
    }

    /**
     * 模糊查询
     * @param flag
     * @return
     */
    @RequestMapping("/query")
    public Msg queryByFlag(String flag){
        if (flag==null){
            return Msg.fail().add("error","参数为空");
        }
        return ALToHMUtil.listTomap(goodServiceS.queryByUser(flag));
    }


    @RequestMapping("/getSimFrGoods")
    public  Msg getSimFreeGoods(@RequestParam(value = "pageNum", defaultValue = "0") int pageNum,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        return ALToHMUtil.listTomap(goodServiceS.getSimFreeGoods(pageNum,pageSize));
    }

    /**
     * 5条一页,我的发布
     * @param openid
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/getMyIssGoods")
    public Msg getMyIssSimGoods(String openid, @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,@RequestParam(value = "pageSize", defaultValue = "5") int pageSize){
        if (openid==null){
            return Msg.fail().add("error","参数为空");
        }
        return ALToHMUtil.listTomap(goodServiceS.getMySimpleGoods(openid,pageNum,pageSize));
    }

    /**
     * 返回用户发布物品的审核信息
     * @param openid
     * @return
     */
    @RequestMapping("getMyCheckSimGoods")
    public Msg getMyCheckSimGoods(String openid){
        if (openid==null){
            return Msg.fail().add("error","参数为空");
        }
        return ALToHMUtil.listTomap(goodServiceS.getMyCheckSimGoods(openid));
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response){
        log.info("请求 exportExcel start ......");

        // 获取用户信息
        List<Goods> list = goodServiceS.getAllGoodsInfo();

        try {
            // 设置响应输出的头类型及下载文件的默认名称
            String fileName = new String("demo信息表.xls".getBytes("utf-8"), "ISO-8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=gb2312");

            //导出
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Goods.class, list);
            workbook.write(response.getOutputStream());
            log.info("请求 exportExcel end ......");
        } catch (IOException e) {
            log.info("请求 exportExcel 异常：{}", e.getMessage());
        }
    }


}
